﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using RemoteTestRunner.BusinessLogic.Properties;
using RemoteTestRunner.BusinessLogic.Views;

namespace RemoteTestRunner.BusinessLogic.ViewModels
{
    public class OptionsViewModel : INotifyPropertyChanged//, IRtrSettings
    {
        private IWpfDialogWindow _dialogWindow;
        private IFileOpenDialogService _fileOpenDialogService;
        private IFolderBrowserDialogService _folderBrowserDialogService;

        public OptionsViewModel()
        {
            OkCommand = new RelayCommand(OkCommandExecute);
            CancelCommand = new RelayCommand(CancelCommandExecute);
            DataFileCommand = new RelayCommand(DataPathCommandExecute);
            MSTestPathCommand = new RelayCommand(MsTestPathCommandExecute);
            WorkingFolderCommand = new RelayCommand(WorkingFolderCommandExecute);
        }

        public ICommand OkCommand { get; set; }
        public ICommand CancelCommand { get; set; }
        public ICommand DataFileCommand { get; set; }
        public ICommand MSTestPathCommand { get; set; }
        public ICommand WorkingFolderCommand { get; set; }

        public IRtrSettings RtrSettings { get; private set; }

        #region FilterTests (INotifyPropertyChanged Property)

        public bool FilterTests
        {
            get { return Settings.Default.FilterTests; }
            set
            {
                if (Settings.Default.FilterTests != value)
                {
                    Settings.Default.FilterTests = value;
                    OnPropertyChanged("FilterTests");
                }
            }
        }

        #endregion

        public void Start(
            IWpfDialogWindow dialogWindow,
            IFileOpenDialogService fileOpenDialogService,
            IFolderBrowserDialogService folderBrowserDialogService,
            IRtrSettings rtrSettings)
        {
            _dialogWindow = dialogWindow;
            _fileOpenDialogService = fileOpenDialogService;
            _folderBrowserDialogService = folderBrowserDialogService;
            RtrSettings = rtrSettings;
        }

        private void OkCommandExecute(object parameter)
        {
            Save();
            _dialogWindow.CloseDialog(true);
        }

        private void CancelCommandExecute(object parameter)
        {
            Cancel();
            _dialogWindow.CloseDialog(false);
        }

        private void DataPathCommandExecute(object parameter)
        {
            string previousFileName = System.IO.Path.GetFullPath(RtrSettings.DataPath);
            string fileName = _fileOpenDialogService.ShowFileOpenDialog(previousFileName, "RTR", ".xml", "RTR Config (.xml)|*.xml");

            if (!string.IsNullOrEmpty(fileName))
            {
                RtrSettings.DataPath = fileName;
            }
        }

        private void MsTestPathCommandExecute(object parameter)
        {
            string previousFileName = System.IO.Path.GetFullPath(RtrSettings.MsTestPath);
            string fileName = _fileOpenDialogService.ShowFileOpenDialog(previousFileName, "MSTest", ".exe", "MSTest (mstest.exe)|mstest.exe");

            if (!string.IsNullOrEmpty(fileName))
            {
                RtrSettings.MsTestPath = fileName;
            }
        }

        private void WorkingFolderCommandExecute(object parameter)
        {
            string previousFolder = System.IO.Path.GetFullPath(RtrSettings.WorkingFolder);
            string folder = _folderBrowserDialogService.ShowFolderBrowserDialog(previousFolder);

            if (!string.IsNullOrEmpty(folder))
            {
                RtrSettings.WorkingFolder = folder;
            }
        }

        public void Save()
        {
            RtrSettings.Save();
        }

        public void Cancel()
        {
            RtrSettings.Cancel();
        }

        #region TestRunMode (INotifyPropertyChanged Property)

        public RunMode TestRunMode
        {
            get
            {
                return RtrSettings.UseRunnerService ? RunMode.Server : RunMode.Local;
            }

            set
            {
                if (value == RunMode.Server)
                {
                    RtrSettings.UseRunnerService = true;
                    OnPropertyChanged("TestRunMode");
                }
                else
                {
                    RtrSettings.UseRunnerService = false;
                    OnPropertyChanged("TestRunMode");
                }
            }
        }

        #endregion

        #region INotifyPropertyChanged values

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }

    public enum RunMode
    {
        Local,
        Server
    }
}